package cn.signalpha.modules.shop.controller.seller;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.log.exception.ServiceException;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.core.tool.utils.StringPool;
import cn.signalpha.core.tool.utils.StringUtil;
import cn.signalpha.modules.shop.dto.PublicityGoodsDTO;
import cn.signalpha.modules.shop.entity.Goods;
import cn.signalpha.modules.shop.entity.Publicity;
import cn.signalpha.modules.shop.entity.PublicityGoods;
import cn.signalpha.modules.shop.service.GoodsService;
import cn.signalpha.modules.shop.service.PublicityGoodsService;
import cn.signalpha.modules.shop.service.PublicityService;
import cn.signalpha.modules.shop.vo.PublicityGoodsVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/sys-seller/publicity/goods")
@Api(value = "营销推广商品", tags = "营销推广商品")
public class SpiPublicityGoodsController extends BaseController {

	private final GoodsService goodsService;
	private final PublicityService publicityService;
	private final PublicityGoodsService publicityGoodsService;

    @GetMapping("/page")
    @ApiImplicitParams({
        @ApiImplicitParam(name="publicityId", dataType = "string", value = "营销推广id"),
		@ApiImplicitParam(name="publicityType", dataType = "string", value = "营销推广类型"),
		@ApiImplicitParam(name="goodsName", dataType = "string", value = "商品标题"),
		@ApiImplicitParam(name="goodsType", dataType = "string", value = "商品类型"),
    })
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<PublicityGoodsVO>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
        return R.data(publicityGoodsService.selectPublicityGoodsPage(Condition.getPage(query), params));
    }

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<PublicityGoods> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(publicityGoodsService.getById(id));
	}

	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "传入publicityGoods")
	public R save(@Valid @RequestBody PublicityGoodsDTO.SaveDTO saveDTO) {
		Publicity publicity = publicityService.getById(saveDTO.getPublicityId());
		Assert.isNull(publicity, "活动不存在");
		Date date = new Date();
		Func.toLongList(saveDTO.getGoodsIds()).forEach(goodsId -> {
			Goods goods = goodsService.getById(goodsId);
			Assert.isNull(goods, "商品不存在");
			if(publicityGoodsService.isExists(saveDTO.getPublicityId(), goods.getId())){
				throw new ServiceException(StringUtil.format("商品{}重复", goods.getName()));
			}

			PublicityGoods publicityGoods = new PublicityGoods();
			publicityGoods.setPublicityId(publicity.getId());
			publicityGoods.setPublicityType(publicity.getType());
			publicityGoods.setGoodsId(goods.getId());
			publicityGoods.setConfig(StringUtil.isBlank(saveDTO.getConfig()) ? StringPool.EMPTY_JSON : saveDTO.getConfig());
			publicityGoods.setBeginTime(publicity.getBeginTime());
			publicityGoods.setEndTime(publicity.getEndTime());
			publicityGoods.setCreateTime(date);
			publicityGoodsService.save(publicityGoods);
		});
		return R.status(true);
	}

	@PostMapping("/update")
	@ApiOperation(value = "编辑", notes = "传入publicityGoods")
	public R update(@Valid @RequestBody PublicityGoodsDTO.UpdateDTO publicityGoodsDTO) {
		PublicityGoods publicityGoods = publicityGoodsService.getById(publicityGoodsDTO.getId());
		Assert.isNull(publicityGoods, MsgConstant.OBJECT_NULL);
		return R.status(publicityGoodsService.updateById(BeanUtil.copy(publicityGoodsDTO, PublicityGoods.class)));
	}

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		Func.toLongList(ids).forEach(id -> {
			PublicityGoods publicityGoods = publicityGoodsService.getById(id);
			Assert.isNull(publicityGoods, MsgConstant.OBJECT_NULL);
			publicityGoodsService.removeById(id);
		});
		return R.status(true);
	}

}
